<template>
  <div>
    <div>
      请输入表名前缀进行批量生成（如:shop_*），或完整表名进行单表生成；
      为了避免覆盖现有源码，请自行先备份再操作？
    </div>
    <br>
    <div>
      <el-input v-model="mistTablename" placeholder="表名前缀" type="mini" style="width: 200px" />
      <el-button type="primary" @click="handlePreview">扫库生成预览菜单</el-button>
      <el-button type="primary" @click="handleBuildCode">扫库生成源码</el-button>
    </div>
    <br>
    <div>{{ logsHead }}</div>
    <div v-for="(logTable,index) in logTableList" :key="index">
      <p>{{ logTable.tableName }}</p>
      <div v-for="(log,i) in logTable.logs" :key="i">
        <span style="line-height:20px; margin-left: 10px;">{{ log.content }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { buildCodeApi, addTemplatetableApi, addTemplatetableColumnApi, gen2LevelMenuApi } from './tmpt-list-api'
export default {
  name: 'BuildCode',
  data() {
    return {
      mistTablename: 'test_*',
      logsHead: '',
      logTableList: [
        /* { tableName: 'test_logs1',
          logs: [
            { content: 'log11' },
            { content: 'log12' }]
        }*/
      ],
      preview_menu_id: 667,
      coding_menu_id: 668
    }
  },

  methods: {
    // 源码模式
    handleBuildCode() {
      this.buildCode('code')
    },
    // 预览模式
    handlePreview() {
      this.buildCode('')
    },

    buildCode(mode) {
      buildCodeApi({ mistTablename: this.mistTablename }).then(result => {
        var resultList = result.list
        var resultListLen = resultList.length
        this.logsHead = '共发现' + resultListLen + '张表进行处理'
        this.logTableList = []

        for (let i = 0; i < resultListLen; i++) {
          const table = resultList[i]
          const tableName = table.tableName
          // initLogs(tableName)
          this.logTableList.push({ tableName: tableName })
        }

        // 调用业务接口
        for (let i = 0; i < resultListLen; i++) {
          const table = resultList[i]
          // const tableName = table.tableName
          // let primaryKey = table.primaryKey
          // showInLogs(tableName, tableName)
          // add template table
          this.addTemplatetable(table, mode)
        }
      })
    },

    // add template table
    addTemplatetable(table, mode) {
      const tableName = table.tableName
      const primaryKey = table.primaryKey
      const data = {}
      data.keyRandom = tableName + '_' + Math.floor(Math.random() * 100000000)
      data.tableName = tableName
      data.tableNameShow = tableName
      data.primaryKey = primaryKey
      data.foreignKey = ''
      data.mode = 1
      data.tableAliasName = 't1'
      data.hasAddBtn = 1
      data.hasDownloadReportBtn = 1
      data.hasEditBtn = 2
      data.hasHistoryBtn = 0
      data.tableNameSql = tableName + ' t1'
      data.width4AddDialog = 0
      data.height4AddDialog = 0
      data.style4AddDialog = 'width:700px;height:500px;'
      data.isdialog4AddDialog = 1
      data.hasCheckbox = 0
      data.deleteFlag = 0
      data.chartWidth = 0
      data.chartHeight = 0
      addTemplatetableApi(data).then(result => {
        const responseCode = result.code
        const responseContent = result.content
        const a1TemplateTableId = result.map.a1TemplateTableId
        this.showInLogs("增加'模板表'结果：" + responseContent + '   a1TemplateTableId=' + a1TemplateTableId, tableName, responseCode)
        // add template table coloumn
        this.addTemplatetableColumn(a1TemplateTableId, data, mode)
      })
    },
    // 日志框 清空
    clearLogs() {
      this.logTableList = []
    },

    // add template table column
    addTemplatetableColumn(a1TemplateTableId, data1, mode) {
      var data = {}
      data._id = a1TemplateTableId
      addTemplatetableColumnApi(data).then(result => {
        this.showInLogs("'模板列'自动扫库结果：" + result.content, data1.tableName, result.code)

        // generator 2 level menu 预览模式
        // 627 is 预览菜单的 menuId
        this.gen2LevelMenu('datagrid', '预览模式', data1.tableName + '_', this.preview_menu_id, a1TemplateTableId, data1)

        // generator 2 level menu 源码模式
        // 628 is 源码菜单的 menuId
        if (mode === 'code') {
          this.gen2LevelMenu('datagrid-sourcecode', '源码模式', data1.tableName, this.coding_menu_id, a1TemplateTableId, data1)
        }
      })
    },
    // generator 2 level menu
    gen2LevelMenu(pageType, modeStr, menu2Name, menu1Id, a1TemplateTableId, data1) {
      const data = {
        'menu1Id': menu1Id,
        '_id': a1TemplateTableId,
        'pageType': pageType,
        'menu2Name': menu2Name,
        'keyRandom': data1.keyRandom
      }
      gen2LevelMenuApi(data).then(result => {
        // this.showInLogs("创建二级菜单'" + modeStr + "'结果：" + result.content, data1.tableName, result.code)
        this.showInLogs(modeStr + ':生成结果：' + result.content, data1.tableName, result.code)
      })
    },

    // 日志框 加入信息
    showInLogs(text, tableName, responseCode) {
      console.log(tableName + '-' + text)
      this.logTableList.forEach((item, index, _self) => {
        if (item.tableName === tableName) {
          let logs = []
          if (item.logs !== undefined) {
            logs = item.logs
          }
          const log = {}
          log.content = text
          logs.push(log)

          item.logs = logs
          this.$forceUpdate()

          return
        }
      })
      // 失败了
      /* if (responseCode === 0) {
        $('#' + tableName).append('<span style="line-height:20px; color: #ff6252;">' + text + '</span><br/>')
      } else {
        $('#' + tableName).append('<span style="line-height:20px;">' + text + '</span><br/>')
      }*/
    }
    /*,
    // init a div content, the id is "tablename"
    initLogs(tableName) {
      $('#logsDiv').append("<div id='" + tableName + "' style='border:1px solid #dddddd; margin:3px;padding:3px;'  />")
    },
    showInLogsHead(text) {
      $('#logsHead').append('<span style="line-height:20px;">' + text + '</span><br/>')
    }
    */

  }
}
</script>

<style scoped>

</style>
